package fr.noe.amicaleloisirs.concours.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;

import fr.noe.amicaleloisirs.concours.interfaces.CallbackHandler;
import fr.noe.amicaleloisirs.concours.utils.Logger;

public class ConfirmPopUp extends PopupPanel  {

	/**
	 * Créé et affiche une PopUp d'info.
	 *
	 * @param text: le texte à afficher (retour à la ligne : < br >).
	 * @param validerHandler: un click handler permettant de définir un traitement particulier lors du click sur valider.
	 * @param annulerHandler: un click handler permettant de définir un traitement particulier lors du click sur annuler.
	 */
	public ConfirmPopUp(final String text, final CallbackHandler callback) {
		super(false);
		setGlassEnabled(true);
		setModal(true);
		//setSize("300px", "200px");

		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(10);
		setWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");

		String[] lines = text.split("<br>");
		for (String line : lines) {
			Label lblNewLabel = new Label(line);
			lblNewLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			verticalPanel.add(lblNewLabel);
			verticalPanel.setCellHorizontalAlignment(lblNewLabel, HasHorizontalAlignment.ALIGN_CENTER);
			verticalPanel.setCellVerticalAlignment(lblNewLabel, HasVerticalAlignment.ALIGN_MIDDLE);
			//lblNewLabel.setSize("100%", "80%");
		}

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("100%");
		verticalPanel.setCellHorizontalAlignment(horizontalPanel, HasHorizontalAlignment.ALIGN_CENTER);

		Button validerButton = new Button("Valider");
		horizontalPanel.add(validerButton);
		horizontalPanel.setCellHorizontalAlignment(validerButton, HasHorizontalAlignment.ALIGN_CENTER);
		validerButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				log("onValidate()");
				callback.onValidate();
				ConfirmPopUp.this.hide();
				log("onValidate() end");
			}
		});

		Button annulerButton = new Button("Annuler");
		horizontalPanel.add(annulerButton);
		annulerButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				log("onCancel()");
				callback.onCancel();
				ConfirmPopUp.this.hide();
			}
		});
		this.center();
	}
	
	private static void log(String text) {
		Logger.get().logWithClass("ConfirmPopUp", text);
	}

}
